package com.pb.wkflow.api.service.impl;

import com.pb.infra.oth.service.BaseService;
import com.pb.wkflow.api.controller.assembler.mapper.WfSysProcDefFieldDtoMapper;
import com.pb.wkflow.api.controller.request.ProcDefFieldRequest;
import com.pb.wkflow.api.controller.request.ProcDefineCreateRequest;
import com.pb.wkflow.api.entity.WfSysProcDefFieldEntity;
import com.pb.wkflow.api.mapper.WfSysProcDefFieldMapper;
import com.pb.wkflow.api.service.WfSysProcDefFieldService;
import com.pb.wkflow.core.utils.StringUtils;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 流程定义字段配置ServiceImpl
 * @author 苗益辉
 * @date 2023/4/17 16:11
 */
@Service
public class WfSysProcDefFieldServiceImpl extends BaseService implements WfSysProcDefFieldService {

    /**
     * wfSysProcDefFieldDtoMapper
     */
    private WfSysProcDefFieldDtoMapper wfSysProcDefFieldDtoMapper;

    /**
     * repositoryService
     */
    @Autowired
    private RepositoryService repositoryService;

    /**
     * wfSysProcDefFieldMapper
     */
    @Autowired
    private WfSysProcDefFieldMapper wfSysProcDefFieldMapper;

    public WfSysProcDefFieldServiceImpl() {
        this.wfSysProcDefFieldDtoMapper = WfSysProcDefFieldDtoMapper.INSTANCE;
    }

    /**
     * 新增或修改流程定义字段配置
     * @param request request
     */
    @Override
    public void addOrUpdate(ProcDefFieldRequest request) {
        String cropId = this.getCurrentUserInfo().getCorpId();
        request.setTenantId(cropId);
        WfSysProcDefFieldEntity wfSysProcDefFieldEntity = wfSysProcDefFieldMapper.selectOne(request.getProcDefKey(), cropId);
        if (null == wfSysProcDefFieldEntity) {
            wfSysProcDefFieldEntity = wfSysProcDefFieldDtoMapper.assemblerCreate(request);
            wfSysProcDefFieldMapper.insert(wfSysProcDefFieldEntity);
        } else {
            wfSysProcDefFieldEntity = wfSysProcDefFieldDtoMapper.assemblerDTOToEntity(request);
            wfSysProcDefFieldMapper.update(wfSysProcDefFieldEntity);
            wfSysProcDefFieldMapper.updateProcInfo(wfSysProcDefFieldEntity.getProcDefName(), wfSysProcDefFieldEntity.getProcDefKey());
            wfSysProcDefFieldMapper.updateProcDef(wfSysProcDefFieldEntity.getProcDefName(), wfSysProcDefFieldEntity.getProcDefKey());
        }
    }

    @Override
    public void add(ProcDefineCreateRequest request) {
        WfSysProcDefFieldEntity wfSysProcDefFieldEntity = wfSysProcDefFieldDtoMapper.assemblerCreate(request);
        wfSysProcDefFieldMapper.insert(wfSysProcDefFieldEntity);
    }

    /**
     * 查询流程定义字段配置
     * @param request request
     * @return WfSysProcDefFieldEntity
     */
    @Override
    public WfSysProcDefFieldEntity findByKey(ProcDefFieldRequest request) {
        String cropId = this.getCurrentUserInfo().getCorpId();
        if (request.getProcDefKey() == null) {
            return new WfSysProcDefFieldEntity();
        }
        WfSysProcDefFieldEntity wfSysProcDefFieldEntity = wfSysProcDefFieldMapper.selectOne(request.getProcDefKey(), cropId);
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().
                processDefinitionKey(request.getProcDefKey()).tenantIdIn(cropId).latestVersion().singleResult();
        if (wfSysProcDefFieldEntity == null ) {
            wfSysProcDefFieldEntity = new WfSysProcDefFieldEntity();
        }
        if (processDefinition != null) {
            if (StringUtils.isBlank(wfSysProcDefFieldEntity.getProcDefKey())) {
                wfSysProcDefFieldEntity.setProcDefKey(processDefinition.getKey());
            }
            if (StringUtils.isBlank(wfSysProcDefFieldEntity.getProcDefName())) {
                wfSysProcDefFieldEntity.setProcDefName(processDefinition.getName());
            }
            if (StringUtils.isBlank(wfSysProcDefFieldEntity.getClassify())) {
                wfSysProcDefFieldEntity.setClassify(processDefinition.getCategory());
            }
        }
        return wfSysProcDefFieldEntity;
    }
}
